<template>
    <div class="header">
        <div class="inner">
            <div class="left w">
                <img class="icon" src="@assets/day.png" alt="">
                <span class="pr20">
                    {{ state.day }}
                </span>
                <span class="pr20">
                    {{ state.week }}
                </span>
                <span class="line"></span>
                <span class="pl10">
                    {{ state.time }}
                </span>
            </div>
            <div class="title">
                班长管家数字驾驶舱
            </div>
            <!-- <img src="@assets/title.png" alt="" class="title"> -->
            <div class="right w">
                <img :src="weatherIcon" alt="" class="weatherIcon">
                <span class="pl20 pr20">{{ state.weatherInfo.weather }}</span>
                <span class="line"></span>
                <span class="pl20">{{ state.weatherInfo.temperature }}℃</span>
            </div>
        </div>
    </div>
</template>
<script setup>
import dayjs from 'dayjs'
import isoWeek from 'dayjs/plugin/isoWeek'
import aMapRequest from '/@/utils/aMapRequest'
import dw from '@assets/dw.png';//雾
import dy from '@assets/dy.png';//多云
import dyu from '@assets/dyu.png';//雨
import qt from '@assets/qt.png';//晴天
import scb from '@assets/scb.png';//沙尘暴
import xue from '@assets/xue.png';//雪
import yt from '@assets/yt.png';//阴天
import { reactive, computed } from 'vue';
dayjs.extend(isoWeek)
const state = reactive({
    time: '',
    weatherInfo: ''
})
const weatherIcon = computed(() => {
    const texts = ['雾', '云', '雨', '晴', '沙', '雪', '阴']
    const icons = [dw, dy, dyu, qt, scb, xue, yt]
    return icons[texts.findIndex(item => state.weatherInfo.weather?.indexOf(item) > -1)] || qt
})
const weeksMap = {
    1: '星期一',
    2: '星期二',
    3: '星期三',
    4: '星期四',
    5: '星期五',
    6: '星期六',
    7: '星期日',
}
const getCurrentTime = () => {
    const getTime = () => {
        state.time = dayjs().format('HH:mm:ss')
        state.day = dayjs().format('YYYY-MM-DD')
        state.week = weeksMap[dayjs().isoWeekday()]
    }
    getTime()
    setInterval(() => {
        getTime()
    }, 250);
}
getCurrentTime()
const amapKey = '3357d0aeffde42469e47f98f9fe9c978'
const getWeather = () => {
    aMapRequest({
        url: `/v3/ip?key=${amapKey}`,
        method: 'get'
    }).then(res => {
        aMapRequest({
            url: `/v3/weather/weatherInfo?key=${amapKey}&city=${res.adcode}`,
            method: 'get'
        }).then(res => {
            console.log(res, 'info')
            state.weatherInfo = res.lives[0];
        })
    })
}
getWeather()
// 5分钟更新一次天气
setInterval(() => {
    getWeather()
}, 5 * 60 * 1000);

</script>
<style lang="scss" scoped>
.header {
    height: 170px;
    background-image: url(@assets/headerBg.png);
    background-size: cover;
    padding-top: 26px;
    box-sizing: border-box;

    .inner {
        display: flex;
        justify-content: space-between;

        .title {
            display: block;
            // width: 456px;
            flex: 1;
            font-family: '庞门正道';
            font-size: 48px;
            font-weight: 600;
            text-align: center;
            background: linear-gradient(180deg, rgba(255, 255, 255, 1) 0%, rgba(181, 255, 237, 1) 100%);
            background-clip: text;
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            height: 50px;
            line-height: 1;
            letter-spacing: 4px;
            // color: #fff;
            white-space: nowrap;
        }

        .w {
            width: 20%;
            display: flex;
            align-items: center;
            color: #fff;
            font-size: 18px;
            white-space: nowrap;

            .pr20 {
                padding-right: 20px;
            }

            .pl20 {
                padding-left: 20px;
            }

            .pl10 {
                padding-left: 10px;
            }

            .line {
                height: 30px;
                width: 2px;
                display: inline-block;
                background: linear-gradient(180deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 1) 54%, rgba(255, 255, 255, 0) 100%);
            }
        }

        .left {
            justify-content: flex-start;
            padding-left: 30px;

            .icon {
                $s: 18px;
                width: $s;
                height: $s;
                margin-right: 5px;
            }
        }

        .right {
            justify-content: flex-end;
            padding-right: 30px;

            .weatherIcon {
                width: 32px;
                height: 22px;
                object-fit: contain;
            }
        }
    }
}
</style>